<template>
  <div class="wrap">
    <div class="menuNav" @mouseleave="currentIndex = 'flag'">
      <!-- 菜单栏 -->
      <div class="menus-body">
        <div class="menus">
          <div
            @mouseover="currentIndex=index"
            :class="['menus-item',{active:currentIndex===index}]"
            v-for="(item,index) in cities"
            :key="index"
          >
            <span>{{item.type}}</span>
            <em class="el-icon-arrow-right"></em>
          </div>
        </div>
      </div>
      <!-- 详情内容 -->
      <div class="content" v-if="currentIndex != 'flag'">
        <div
          class="contentItem"
          v-if="currentIndex==index"
          v-for="(item,index) in cities"
          :key="index"
        >
          <div
            class="children"
            v-for="(value,i) in item.children"
            :key="i"
            @click="$router.push('/post?city='+value.city)"
          >
            <div class="num">{{i+1}}</div>
            <span class="cityName">{{value.city}}</span>
            <p class="desc">{{value.desc}}</p>
          </div>
        </div>
      </div>
    </div>
    <!-- 推荐城市模块 -->
    <div class="aside-recommend">
      <h4 class="aside-title">推荐城市</h4>
      <!-- 左侧轮播图 -->
      <el-carousel height="165px" indicator-position="none">
        <el-carousel-item v-for="(item,index) in loopList" :key="index">
          <nuxt-link :to="`/post/detail?id=${item.id}`" class="aside-recommend-item">
            <h3 class="small">{{ item.title }}</h3>
            <img :src="item.images[0]" />
          </nuxt-link>
        </el-carousel-item>
      </el-carousel>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cities: [],
      currentIndex: 'flag',  //标记当前显示的菜单项  
      loopList: [],           //轮播数组
      postList: []
    }
  },
  mounted() {
    // console.log(this.currentIndex);
    //左排行-- 城市数据
    this.getCities()

    //获取 数据 for 轮播
    this.$axios({
      url: '/posts',
    })
      .then(res => {
        // console.log(res);
        this.postList = res.data.data

        this.loopList = [...this.postList].sort((a, b) => { return b.like - a.like })
        this.loopList = this.loopList.splice(0, 4)
        // console.log(this.loopList);
      })

  },
  methods: {
    // 获取城市菜单列表  http:127.0.0.1:1337/posts/cities
    getCities() {
      this.$axios({
        url: '/posts/cities',
      })
        .then(res => {
          console.log(res);
          this.cities = res.data.data
        })
        .catch(err => {
          console.dir(err)
        })
    }
  }
  // watch: {
  //   currentIndex() {
  //     console.log(this.currentIndex);
  //   }
  // }
}
</script>
<style lang="less" scoped>
.menuNav {
  width: 260px;
  position: relative;
  .menus {
    border-left: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 999;
    position: relative;
    .menus-item {
      height: 40px;
      padding: 0 20px;
      font-size: 14px;
      border-bottom: 1px solid #ddd;
      border-right: 1px solid #ddd;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .el-icon-arrow-right {
        font-size: 18px;
        color: #999;
      }
      &.active {
        border-right: 1px solid #fff;
      }
    }
    .menus-item:hover {
      color: #ffa500;
      /deep/ .el-icon-arrow-right {
        color: #ffa500;
      }
    }
  }
  //   详情内容样式
  .content {
    position: absolute;
    width: 350px;
    height: 190px;
    left: 259px;
    top: 0;
    background-color: #fff;
    border: 1px solid #ddd;
    padding-top: 5px;
    z-index: 2;
    .contentItem {
      cursor: pointer;
      .children {
        display: flex;
        align-items: center;
        padding: 8px 10px 8px 20px;
        height: 20px;
        background-color: #fff;
        .num {
          color: #ffa500;
          font-size: 23px;
          font-style: italic;
          margin-right: 15px;
        }
        .cityName {
          color: #ffa500;
          font-size: 14px;
        }
        .cityName:hover,
        .desc:hover {
          text-decoration: underline;
        }
        .desc {
          color: #999;
          font-size: 14px;
          margin-left: 15px;
        }
      }
    }
  }
}
.aside-recommend {
  margin-top: 20px;
  .aside-title {
    font-weight: 400;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
    margin-bottom: 10px;
  }
  .aside-recommend-item {
    position: relative;
    img {
      width: 100%;
      height: 170px;
    }
    .small {
      width: 100%;
      position: absolute;
      bottom: 0;
      padding: 10px;
      font-weight: 400;
      font-size: 15px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
      z-index: 9;
      background-color: rgba(255, 255, 255, 0.3);
    }
  }
}
</style>